/****WEB-mysql-服务器****/
// 加载Express模块
const express = require("express");
// 创建服务器
const server = express();

// 加载CORS模块
const cors = require("cors");

// 使用CORS中间件解决跨域
server.use(
  cors({
    origin: ["http://localhost:8080"],
    credentials: true,
  })
);
// 解决hash模式异常跳转问题
const history = require("connect-history-api-fallback");
server.use(history());

server.use(express.static("dist"));
const port = 3000;
server.listen(port, () => {
  console.log("server is running, port:" + port);
});

// post中间件
server.use(
  express.urlencoded({
    extended: false,
  })
);

// 引入接口路由器：/router
const userRouter = require("./router/user");
server.use("/v1/user", userRouter);
const adminRouter = require("./router/admin");
server.use("/v1/admin", adminRouter);
const artRouter = require("./router/article");
server.use("/v1/article", artRouter);
const beauRouter = require("./router/beau");
server.use("/v1/beau", beauRouter);
const deviceRouter = require("./router/device");
server.use("/v1/device", deviceRouter);
const urlRouter = require("./router/imgurl");
server.use("/v1/imgurl", urlRouter);
const cartRouter = require("./router/cart");
server.use("/v1/cart", cartRouter);
const docRouter = require("./router/doctor");
server.use("/v1/doctor", docRouter);
const evalRouter = require("./router/evaluate");
server.use("/v1/eval", evalRouter);
const kindRouter = require("./router/kind");
server.use("/v1/kind", kindRouter);
const orderRouter = require("./router/order");
server.use("/v1/order", orderRouter);
const petsRouter = require("./router/pets");
server.use("/v1/pets", petsRouter);
const rRouter = require("./router/reserve");
server.use("/v1/reserve", rRouter);
const supplyRouter = require("./router/supply");
server.use("/v1/supply", supplyRouter);

//错误处理中间件——在所有的路由器后边，拦截所有的错误
server.use((err, req, res, next) => {
  console.log(err);
  res.send({ code: 500, msg: "服务器端错误" });
});
